<?php
/**
 * @link https://www.kancloud.cn/cleverstone/ymb2
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace backend\controllers;

use builder\base\BaseController;
use builder\helper\App;
use builder\helper\H5;
use common\models\SystemMenu;
use common\models\SystemMenuService;

/**
 * 菜单
 * @author cleverstone
 * @since ym2.0
 */
class MenuController extends BaseController
{
    public $actionVerbs = [
        'index' => ['GET'],
        'update' => ['GET', 'POST'],
    ];

    /**
     * 菜单列表
     * @return string
     * @throws \Throwable
     */
    public function actionIndex()
    {
        $viewBuilder = $this->viewBuilder;

        $treeTable = $viewBuilder->getTreeTable(['id' => 'Menu_Index']);
        if ($this->isAjax) {
            $treeTable->setPage(false)
                ->setQuery(function () {
                    return SystemMenuService::tree();
                })
                ->setTtColumns([
                    'id',
                    'Name_Str' => function ($row) {
                        $result = [];
                        $result[] = $row['icon'] ? H5::tag('span', '', ['class' => $row['icon']]) : '';
                        $result[] = $row['name'];
                        return implode("\n", $result);
                    },
                    'Icon_Str' => function ($row) {
                        return $row['icon'] ? H5::tag('i', '', ['class' => $row['icon']]) : '';
                    },
                    'Category_Str' => function ($row) {
                        return SystemMenu::CONST_CATEGORY_DICT[$row['category']]['text'];
                    },
                    'link',
                    'Dump_Way_Str' => function ($row) {
                        return SystemMenu::CONST_DUMP_WAY_DICT[$row['dump_way']]['text'];
                    },
                    'Desc_Str' => function ($row) {
                        return $row['desc'] ?: '-';
                    },
                    'Is_Quick_Str' => function ($row) {
                        return H5::badge($row['is_quick'] ? '是' : '否', $row['is_quick'] ? 'primary' : 'gray');
                    },
                    'created_at',
                    'updated_at',
                ]);
        } else {
            App::setViewBreadcrumb([
                ['name' => '系统管理'],
                ['name' => '菜单列表', 'url' => 'menu/index'],
            ]);
            $treeTable->setTitle('菜单列表')
                ->setPage(false)
                ->setMaxHeight(800)
                ->setTreeViewOption([
                    'showIcon' => false,
                ])
                ->registerPointcut(
                    H5::alert("◆ 通过 `配置文件` 和 `定义文件` 可实现菜单新增、编辑、排序。\n ◆ 菜单 `配置文件` 为 `app/config/menu`。\n ◆ 菜单 `定义文件` 为 `app/menu/*`。", 'normal', false)
                )
                ->setTreeTableHead([
                    $this->treeTableHead->field('id')->title('ID'),
                    $this->treeTableHead->field('Name_Str')->title('名称')->minWidth(300),
                    $this->treeTableHead->field('Icon_Str')->title('图标')->width(60),
                    $this->treeTableHead->field('Category_Str')->title('种类')->width(80),
                    $this->treeTableHead->field('link')->title('权限')->width(200),
                    $this->treeTableHead->field('Dump_Way_Str')->title('视窗')->width(100),
                    $this->treeTableHead->field('Desc_Str')->title('描述')->width(300),
                    $this->treeTableHead->field('Is_Quick_Str')->title('快捷菜单')->width(120),
                    $this->treeTableHead->field('created_at')->title('添加时间')->width(150),
                    $this->treeTableHead->field('updated_at')->title('上次更新')->width(150),
                ])
                ->setTtToolbarCustom([
                    $this->ttToolbarCustom->ajax()->btnClass('btn-primary')->title('一键更新')->route('menu/update')->method(),
                ])
                ->setTreeCustomNameOption(['name' => 'Name_Str'])
                ->setTtToolbarExpandAll();
        }


        return $viewBuilder->render();
    }

    /**
     * 一键更新
     * @return \yii\web\Response
     * @throws \yii\base\InvalidConfigException
     */
    public function actionUpdate()
    {
        $result = \Yii::$app->rbacManager->updateSystemMenu();
        if ($result === true) {
            return $this->asOk('系统菜单已从本地获取更新。');
        }

        return $this->asFail($result);
    }
}